<template>
  <div>
    <!-- <TopFilter :list="fiterList" :cols="3" :rows="2" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter> -->
    <FilterTable
      ref="prizeOutbound"
      :columns="columns"
      columnsRef="partPartOtherPrizeOutboundIndex"
      :datakey="'rows'"
      selectionType="single"
      :onRowSelectChange="columns => (this.selectRow = columns)"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :onColumnsChange="columns => (this.columns = columns)"
      exportFileName="奖品执行出库.xlsx"
    >
      <template slot="controls">
        <AjaxButton size="small" type="primary" icon="el-icon-plus" :click="saveClick">新建</AjaxButton>
        <AjaxButton size="small" type="primary" :click="deleteHandle">删除</AjaxButton>
        <AjaxButton size="small" type="primary" :click="changeHandle">刷新</AjaxButton>
        <AjaxButton size="small" type="primary" :click="outHandle">执行出库</AjaxButton>
        <!-- <AjaxButton size="small" type="primary" :click="() => {}">导出</AjaxButton>
        <AjaxButton size="small" type="primary" :click="() => {}">打印</AjaxButton> -->
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="visible" :title="dialogTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <AddPrizeOutbound :clickType="clickType" :outOrderEmit="outOrderEmit" :storageList="storageList" @saveHandle="saveHandle" @close="closeAddPrize" />
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { queryShortPart, saveShortPart } from '@/api/part/partOther/lackDetail';
import { findStorageCode } from '@/api/repair/basicParameters';
import { getStorageList, getAllUser } from '@/api/part/common';
import dictCode from '@/utils/dictCode';
import moment from 'moment';
import AddPrizeOutbound from './addPrizeOutbound';
import { saveOutOrder, outOrderList, outStorage, deleteById } from '@/api/part/partOther/prizeOutbound';
import { notifyAction, confirmAction } from '@/utils/index.js';
import Cookies from 'js-cookie';
export default {
  name: 'prizeOutbound',
  components: { AddPrizeOutbound },
  mixins: [dictionary],

  data() {
    return {
      fetchParams: {
        xhrAbort: true
      },
      columns: this.createTableColumns(),
      fiterList: this.createFilterList(),
      fetchApi: outOrderList,
      partColumns: [],
      userList: [],
      serviceAdvisorData: [],
      visible: false,
      dialogTitle: '新建奖品执行出库',
      storageList: [],
      selectRow: [],
      outOrderEmit: {}, // 编辑对象
      clickType: 'edit'
    };
  },
  mounted() {
    this.getUserList();
    this.BaseTable = this.$refs.prizeOutbound.$refs.pageTable;
  },
  created() {
    this.getStorage();
  },
  methods: {
    // 编辑操作
    emitFun(val) {
      this.visible = true;
      this.dialogTitle = '编辑奖品执行出库';
      this.clickType = 'edit';
      this.outOrderEmit = val;
    },
    // 执行出库
    async outHandle() {
      if (this.selectRow.length < 1) {
        return notifyAction('请选中一条数据', 'warning');
      }
      await confirmAction('确定执行出库?');
      let res = await outStorage(this.selectRow[0].id);
      if (res.resultCode == 200) {
        notifyAction('执行出库成功', 'success');
      }
      this.changeHandle();
    },
    // 删除操作
    async deleteHandle() {
      if (this.selectRow.length < 1) {
        return notifyAction('请选中一条数据', 'warning');
      }
      await confirmAction('确定删除该出单?');
      let res = await deleteById(this.selectRow[0].id);
      if (res.resultCode == 200) {
        notifyAction('删除成功', 'success');
      }
      this.changeHandle();
    },
    // 获取人员列表
    async getUserList() {
      let res = await getAllUser();
      this.serviceAdvisorData = res.data.map(x => ({ text: x.employeeName, value: x.userId }));
      res.data.map(x => {
        this.userList[x.userId] = x.employeeName;
      });
    },
    // 翻译
    getStorageName(storageCode) {
      const item = this.storageList.find(x => {
        return x.value == storageCode;
      });
      return item ? item.text : '';
    },
    // 获取仓库列表
    async getStorage() {
      const res = await getStorageList();
      this.storageList = res.data.map(x => ({ text: x.STORAGE_NAME, value: x.STORAGE_CODE }));
      // console.log(this.storageList, '仓库列表');

      // this.topFilterList.find(item => item.fieldName === 'STORAGE_CODE').itemList = res.data.map(x => ({ text: x.STORAGE_NAME, value: x.STORAGE_CODE }));
    },
    changeHandle(val) {
      this.fetchParams.xhrAbort = true;
      this.fetchParams = Object.assign({}, val, { xhrAbort: false });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    saveClick(val) {
      this.dialogTitle = '新建奖品执行出库';
      this.clickType = 'add';
      this.visible = true;
    },
    // 新建页面点击保存
    saveHandle(val) {
      // console.log('测试', val);
      this.visible = false;
      this.changeHandle();
      // this.fetchParams.xhrAbort = true;
    },
    closeAddPrize(val, type) {
      this.visible = val;
      // 重置 editData
      // this.editData = {};
      // 执行刷新
      // type === 'refresh' && this.refreshHandle();
    },
    isEmit(val) {},
    createFilterList() {
      return [
        {
          type: 'INPUT',
          label: '出库单号',
          fieldName: 'awardOutNo',
          placeholder: ''
          // readonly: true
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          // sorter: true,
          // filter: true,
          render: (props, h) => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.emitFun(props.row)} disabled={this.isEmit(props)}>
                  编辑
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 100,
          sorter: true,
          // filter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },

        {
          title: '出库单号',
          dataIndex: 'awardOutNo',
          width: 120,
          sorter: true,
          filter: true,
          filterType: 'input'
          //   editable: true,
          //   editType: 'select',
          //   editItems: [{ text: '是', value: 10041001 }, { text: '否', value: 10041002 }]
        },
        {
          title: '兑奖单编号',
          dataIndex: 'awardNo',
          sorter: true,
          filter: true,
          filterType: 'input'
          //   editable: true,
          //   editType: 'select',
          // editItems: [
          //   { text: '是', value: 10041001 },
          //   { text: '否', value: 10041002 }
          // ]
        },
        {
          title: '创建日期',
          dataIndex: 'createdAt',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          width: 150,
          filterType: 'date-range'
          // render: props => {
          //   return <span>{this.createDictText(props.row.SHORT_TYPE, '1322')}</span>;
          // }
        },
        {
          title: '客户代码',
          dataIndex: 'customerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户名称',
          dataIndex: 'customerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '联系电话',
          dataIndex: 'IN_OUT_TYPE',
          sorter: true,
          filter: true,
          filterType: 'input'
          // render: props => {
          //   return <span>{this.createDictText(props.row.IN_OUT_TYPE, '8249')}</span>;
          // }
        },
        {
          title: '配件名称',
          dataIndex: 'partName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '待兑奖数量',
          dataIndex: 'outQuantity',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '仓库代码',
          dataIndex: 'storageCode',
          sorter: true,
          filter: true,
          filterType: 'input',
          editable: true,
          editType: 'select',
          editItems: this.storageList
        },
        {
          title: '仓库名称',
          dataIndex: 'storageName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '批号',
          dataIndex: 'batchNo',

          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '库区',
          dataIndex: 'a2',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '库位',
          dataIndex: 'storagePositionCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '备注',
          dataIndex: 'remark',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '公司号',
          dataIndex: 'ownerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
